﻿package smooth.core.canvas.liquidCanvas {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.filters.*;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	public class LiquidCanvasMenu extends Sprite {
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		
		private var initialWidth:int;
		private var initialHeight:int;
		private var initialPosX:int;
		private var initialPosY:int;
		private var initialColor:int;
		private var currentButtonAngle:Number;
		private var closeIcon:Bitmap;
		private var internalRadiusMenu:int;
		private var radiusMenu:int;
		private var backgroundDrawing:Shape = new Shape();	
		
		
		public function LiquidCanvasMenu(posX:int, posY:int, color:int, radius:int) {
			// Init des attributs de l'objet
			this.initialPosX = posX;
			this.initialPosY = posY;
			this.initialColor = color;
			this.radiusMenu = radius;
			this.internalRadiusMenu = 60;
			this.currentButtonAngle = Math.PI/2;
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();	
			// Init des process du composant
			initProcess();
		}
		
		private function initGraphics():void {
			this.x = this.initialPosX;
			this.y = this.initialPosY;
			this.closeIcon = new Bitmap();
			var imageLoader:Loader =new Loader();
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,closeIconLoadComplete);
			imageLoader.load(new URLRequest("../resources/closeRed.png"));
			this.addChild(this.backgroundDrawing);
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.closeIcon.addEventListener(TouchEvent.MOUSE_DOWN, this.doClose);
		}
		
		private function closeIconLoadComplete(event:Event):void {
			closeIcon.bitmapData = event.target.content.bitmapData;
			closeIcon.alpha = 0.5;
			closeIcon.width = 80;
			closeIcon.height = 80;
			closeIcon.x = -40;
			closeIcon.y = -this.radiusMenu-40;
			closeIcon.alpha = 0.8;
			this.addChild(closeIcon);
			// Draw circle for close button
			this.backgroundDrawing.graphics.lineStyle(4,0xFFFFFF, 1);
			this.backgroundDrawing.graphics.beginFill(0xFFFFFF, 0.5);
			this.backgroundDrawing.graphics.drawCircle(0, -this.radiusMenu, 45);
			this.backgroundDrawing.graphics.endFill();
		}
		
		private function doClose(e:TouchEvent):void {
			this.parent.removeComponent();
			e.stopPropagation();
		}
		
		public function addButton(o:Object):void {
			if (o != null) {
				var p:Number = 2 * Math.PI / 8;
				o.x = this.radiusMenu*Math.cos(this.currentButtonAngle-p);
				o.y = -this.radiusMenu*Math.sin(this.currentButtonAngle-p);
				o.initialPosX = o.x;
				o.initialPosY = o.y;
				this.addChild(o);
				this.currentButtonAngle = this.currentButtonAngle-p;
			}
		}
		
	}
	
}